using System;
using BasecampRestAPI;
using NUnit.Framework;
using TestBasecampRestAPI.Fakes;

namespace TestBasecampRestAPI
{
	[TestFixture]
	public class TestProjectCreate : TestProjectBaseFixture
	{
		private const int FakeCategoryId = 1366;

		[Test]
		public void CreateMilestone()
		{
			DateTime deadline = new DateTime(2009, 08, 31);
			FakePerson responsibleParty = new FakePerson { Id = 1482 };
			SetupResponseXml(string.Format("<milestones type=\"array\">{0}</milestones>",
			                               Properties.Resources.FakeMilestone));

			IMilestone milestone = _project.CreateMilestone("Milestone 1", deadline, responsibleParty, true);

			Assert.IsNotNull(milestone);
			AssertGetRequestForAction("milestones/create");
			Assert.AreEqual(HttpVerb.Post, _service.GetRequestResponseElementLastVerb);
			Assert.AreEqual(10808780, milestone.Id);
			Assert.AreEqual("Milestone 1", milestone.Title);
			Assert.AreEqual(deadline, milestone.Deadline);
			Assert.IsTrue(milestone.WantsNotification);
		}

		[Test]
		public void CreateToDoList()
		{
			const string name = "Foo";
			const string description = "The list for the Foo stuff.";
			FakeMilestone milestone = new FakeMilestone { Id = 1256, Title = "Iteration 1" };
			const bool privateList = false;
			const bool trackedList = true;
			string location = ToDoList.PathForId(5454);
			_service.PostRequestGetLocationFakeResult = location;
			SetupResponseXml(Properties.Resources.FakeToDoList);

			IToDoList list = _project.CreateToDoList(name, description, milestone, privateList, trackedList);

			Assert.IsNotNull(list);
			Assert.AreEqual(5454, list.Id);
			Assert.AreEqual(name, list.Name);
			Assert.AreEqual(description, list.Description);
			Assert.AreEqual(privateList, list.Private);
			Assert.AreEqual(trackedList, list.Tracked);

			AssertGetRequestCalledForPath(location);
			AssertPostRequestGetLocationCalledForAction("todo_lists.xml");
		}

		[Test]
		public void CreateCategory()
		{
			const string name = "Poofy Goofs";
			string location = Category.PathForId(FakeCategoryId);
			_service.PostRequestGetLocationFakeResult = location;
			SetupResponseXml(Properties.Resources.FakeCategory);

			ICategory category = _project.CreateCategory(CategoryType.Post, name);

			Assert.IsNotNull(category);
			AssertGetRequestCalledForPath(location);
			Assert.AreEqual(HttpVerb.Get, _service.GetRequestResponseElementLastVerb);
			Assert.AreEqual(FakeCategoryId, category.Id);
			Assert.AreEqual(name, category.Name);
			AssertPostRequestGetLocationCalledForAction("categories.xml");
		}

		[Test]
		public void CreateMessage()
		{
			FakeCategory category = new FakeCategory { Id = FakeCategoryId };
			const string title = "The Poofy Goofs";
			const string body = "This message is about the poofy goofs.";
			const string extendedBody = "This is the extended body.";
			FakePerson[] notifications = new FakePerson[2]
			{ new FakePerson { Id = 2232 }, new FakePerson { Id = 2233 } };
			FakeAttachment[] attachments = new FakeAttachment[0];
			SetupResponseXml(Properties.Resources.FakePost);
			string location = Post.PathForId(4404);
			_service.PostRequestGetLocationFakeResult = location;
			IPost message = _project.CreateMessage(category, title, body, extendedBody, false,
				notifications, attachments);
			Assert.IsNotNull(message);
			Assert.AreEqual(4404, message.Id);
		}

		private void AssertPostRequestGetLocationCalledForAction(string action)
		{
			Assert.IsTrue(_service.PostRequestGetLocationCalled);
			Assert.AreEqual("projects/6969/" + action, _service.PostRequestGetLocationLastRequestPath);
		}
	}
}